package com.zk.service.impl;

import com.zk.dao.UserDao;
import com.zk.model.*;
import com.zk.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class UserServiceImpl implements UserService{


    @Resource
    private UserDao userDao;
//    用户登录
    @Override
    public User queryUser(String name, String password) {
        User user=userDao.selectUser(name,password);
        return user;
    }
//    用户注册
    @Override
    public int addUser(String name, String password, String adress, String phone, String age) {
        int num=userDao.RegisterUser(name,password,adress,phone,age);
        return num;
    }
    //    商品初始化
    @Override
    public Goods queryGoods(Integer id) {
        Goods goods=userDao.selectGoods(id);
        return goods;
    }
//加入购物车
    @Override
    public int addCart(Integer userId,Integer goodId,Integer num) {
       int sum=userDao.addCart(userId,goodId,num);
        return sum;
    }
//购物车展示
    @Override
    public List<Cart> selectCarts(Integer UserId) {
        List<Cart> cartList=userDao.selectCarts(UserId);
        for(Cart cart:cartList){
           Goods goods=userDao.selectGoods(cart.getGoodId());
           cart.setName(goods.getName());
           cart.setImg(goods.getImg());
           cart.setSize(goods.getSize());
           cart.setPrice(goods.getPrice());
           cart.setEndPrice(goods.getPrice()*cart.getNum());
        }
        return cartList;
    }
    //添加订单
    @Transactional
    @Override
    public int addOrder(Integer goodId, Integer userId, String orderId, Integer sum, Integer totalPrice) {
        Integer serial=userDao.selectSerialByGoodsId(goodId);
        Integer stock=userDao.selectGoodsNum(goodId)-sum;
        if (stock<0){
            int i=0/10;
        }
        userDao.updateStock(goodId,stock);
        Integer serial2=userDao.selectSerialByGoodsId(goodId);
        if (serial!=serial2){
            int i=0/10;
        }
        serial2=serial2+1;
        int nn=userDao.updateSerial(goodId,serial2);
        int num=userDao.addOrder(goodId,userId,orderId,sum,totalPrice);
        return num;
    }
//查看订单
    @Override
    public List<Order> selectOrders(Integer userId) {
        List<Order> orderList=userDao.selectOrders(userId);
        for(Order order:orderList){
            Goods goods=userDao.selectGoods(order.getOrderGoodId());
            order.setName(goods.getName());
            order.setImg(goods.getImg());
            order.setSize(goods.getSize());
        }
        return orderList;
    }
//查询商品
    @Override
    public List SelectGoodByName(String name) {
        List<Goods> goods=userDao.selectGoodByName(name);
        return goods;
    }
//修改收货地址
    @Override
    public int updateAdress(String adress, String phone, Integer userId) {
        int num=userDao.updateAdress(adress,phone,userId);
        return num;
    }
//通过id更新用户
    @Override
    public User rennewUser(Integer userId) {
        User user=userDao.renewUser(userId);
        return user;
    }
//添加评价
    @Override
    public int addPj(String text, Integer userId, Integer goodsId) {
        int num=userDao.addPj(text,userId,goodsId);
        return num;
    }
//通过商品id获取图片
    @Override
    public String selectImgById(Integer goodsId) {
        String img=userDao.selectImgById(goodsId);
        return img;
    }
//获取评价
    @Override
    public List<Pj> selectPj() {
        List<Pj> pjList=userDao.selectPj();
        for(Pj pj:pjList){
            pj.setOrderImg(selectImgById(pj.getPjGoodsId()));
        }
        return pjList;
    }
//只看我的评价
    @Override
    public List<Pj> myPj(Integer userId) {
        List<Pj> pjList=userDao.myPj(userId);
        for(Pj pj:pjList){
            pj.setOrderImg(selectImgById(pj.getPjGoodsId()));
        }
        return pjList;
    }
//下单后更新
    @Override
    public void deleteGoods(Integer userId, Integer goodsId) {
        userDao.deleteGoods(userId,goodsId);
    }
}
